﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SmartRfMng.appcode;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;
using Newtonsoft.Json;
using System.Threading;
using System.Globalization;
using BT_Printer;

namespace SmartRfMng
{
    public partial class frmMain : Form
    {
        string langue = CultureInfo.CurrentCulture.ToString();
        private static string strPrnName = "", strPairCode = "";
        

        //软键盘
        [DllImport("coredll.dll")]
        public extern static void SipShowIM(uint isFlag);
        private bool bPluLoad = false;
        private bool CanDoNext = true;
        
        public frmMain()
        {
            InitializeComponent();
            SipShowIM(0x00);//关闭软键盘
            CanDoNext = true;
            SmartVar.ConnectedBT = false;
        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要退出主程序吗？", "客户分销", MessageBoxButtons.YesNo,
                 MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
            if (dr == DialogResult.Yes)
            {
                this.Close();
            }
        }


        private void frmMain_Load(object sender, EventArgs e)
        {
            lblshow.Visible = false;
            bPluLoad = false;
            lblPluLoad.Visible = false;

            //读配置文件
            string sSnyTime = ZT_INI.GetINI("system", "SnyTime", "");

            if (sSnyTime == "1")
            {
                StoreRf rf = new StoreRf();
                string sDateTime = rf.GetServerDate();
                if (sDateTime != "")
                {
                    lblshow.Text ="时间:" + sDateTime;
                    SysDate.SetDateTime(sDateTime);
                    lblshow.Visible = true;
                }
            }
            
            //初始化蓝牙设备
            if (SmartVar.myBT.InitDevice())
            {
                if (langue == "zh-CN")
                    lblPluLoad.Text = "蓝牙设备初始化成功";
                else
                    lblPluLoad.Text = "Init Bluetooth device OK.";
            }
            else
            {
                if (langue == "zh-CN")
                    lblPluLoad.Text = SmartVar.myBT.DeviceMessage_Ch;
                else
                    lblPluLoad.Text = SmartVar.myBT.DeviceMessage_En;
            }
        }

        private void btnwslxs_Click(object sender, EventArgs e)
        {
            if (!bPluLoad)
            {
                lblPluLoad.ForeColor = Color.Red;
                lblPluLoad.Text = "请先进行信息加载";
                lblPluLoad.Visible = true;
                return;
            }

            if (!CanDoNext)
            {
                return;
            }
            CanDoNext = false;

            try
            {
                frmWslXs frmwslxs = new frmWslXs();
                try
                {
                    frmwslxs.ShowDialog();
                }
                finally
                {
                    frmwslxs = null;
                }
            }
            finally
            {
                CanDoNext = true;
            }
        }

        private void btnplu_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要进行信息同步吗？", "客户分销", MessageBoxButtons.YesNo,
                 MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (dr != DialogResult.Yes)
            {
                return;
            }

            if (!CanDoNext)
            {
                return;
            }
            CanDoNext = false;

            try
            {
                string sRtnMsg = string.Empty;
                StoreRf rf = new StoreRf();

                lblshow.Text = "正在进行批发客户信息同步...";
                lblshow.Visible = true;
                this.Refresh();

                bool bResult1 = rf.DealEtpCustInfo(out sRtnMsg);
                lblshow.Visible = false;


                lblshow.Text = "正在进行员工信息同步...";
                lblshow.Visible = true;
                this.Refresh();

                bool bResult2 = rf.DealUsrEmpInfo(out sRtnMsg);
                lblshow.Visible = false;


                lblshow.Text = "正在进行车辆信息同步...";
                lblshow.Visible = true;
                this.Refresh();

                bool bResult3 = rf.DealDspTruckInfo(out sRtnMsg);
                lblshow.Visible = false;


                lblshow.Text = "正在进行商品信息同步...";
                lblshow.Visible = true;
                this.Refresh();

                bool bResult4 = rf.DealPluInfo(out sRtnMsg);
                lblshow.Visible = false;


                if (bResult1 && bResult2 && bResult3 && bResult4)
                {
                    MessageBox.Show("文件下载成功！", "提示", MessageBoxButtons.OK,
                      MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                    return;
                }
                else
                {
                    MessageBox.Show("文件下载失败！", "提示", MessageBoxButtons.OK,
                      MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                    return;
                }
            }
            finally
            {
                CanDoNext = true;
            }
        }

        private void btnPluLoad_Click(object sender, EventArgs e)
        {
            if(bPluLoad)
            {
                lblPluLoad.ForeColor = Color.Blue;
                lblPluLoad.Text = "信息已加载";
                lblPluLoad.Visible = true;
                return;
            }

            if (!CanDoNext)
            {
                return;
            }
            CanDoNext = false;

            try
            {
                lblshow.Text = "正在进行批发客户信息加载...";
                lblshow.Visible = true;
                this.Refresh();

                string FileName = SmartVar.EtpFile;
                if (File.Exists(FileName))
                {
                    FileStream fs = File.OpenRead(FileName);
                    try
                    {
                        byte[] buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);
                        string sEtpXML = System.Text.Encoding.Default.GetString(buffer, 0, buffer.Length);
                        SmartVar.l_EtpCustInfo = JsonConvert.DeserializeObject<List<EtpCustInfo>>(sEtpXML);
                        //bPluLoad = true;

                        lblPluLoad.ForeColor = Color.Blue;
                        lblPluLoad.Text = "批发客户信息已加载";
                        lblPluLoad.Visible = true;
                    }
                    finally
                    {
                        fs.Close();
                    }
                }

                lblshow.Text = "正在进行员工信息加载...";
                lblshow.Visible = true;
                this.Refresh();

                FileName = SmartVar.EmpFile;
                if (File.Exists(FileName))
                {
                    FileStream fs = File.OpenRead(FileName);
                    try
                    {
                        byte[] buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);
                        string sEmpXML = System.Text.Encoding.Default.GetString(buffer, 0, buffer.Length);
                        SmartVar.l_UsrEmployee = JsonConvert.DeserializeObject<List<UsrEmployee>>(sEmpXML);
                        //bPluLoad = true;

                        lblPluLoad.ForeColor = Color.Blue;
                        lblPluLoad.Text = "员工信息已加载";
                        lblPluLoad.Visible = true;
                    }
                    finally
                    {
                        fs.Close();
                    }
                }

                lblshow.Text = "正在进行车辆信息加载...";
                lblshow.Visible = true;
                this.Refresh();

                FileName = SmartVar.TruckFile;
                if (File.Exists(FileName))
                {
                    FileStream fs = File.OpenRead(FileName);
                    try
                    {
                        byte[] buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);
                        string sDspXML = System.Text.Encoding.Default.GetString(buffer, 0, buffer.Length);
                        SmartVar.l_DspTruck = JsonConvert.DeserializeObject<List<DspTruck>>(sDspXML);
                        //bPluLoad = true;

                        lblPluLoad.ForeColor = Color.Blue;
                        lblPluLoad.Text = "车辆信息已加载";
                        lblPluLoad.Visible = true;
                    }
                    finally
                    {
                        fs.Close();
                    }
                }

                lblshow.Text = "正在进行商品信息加载...";
                lblshow.Visible = true;
                this.Refresh();

                FileName = SmartVar.PluFile;
                if (File.Exists(FileName))
                {
                    FileStream fs = File.OpenRead(FileName);
                    try
                    {
                        byte[] buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);
                        string sPluXML = System.Text.Encoding.Default.GetString(buffer, 0, buffer.Length);
                        SmartVar.l_PluInfo = JsonConvert.DeserializeObject<List<PluInfo>>(sPluXML);
                        //bPluLoad = true;

                        lblPluLoad.ForeColor = Color.Blue;
                        lblPluLoad.Text = "商品信息已加载";
                        lblPluLoad.Visible = true;
                    }
                    finally
                    {
                        fs.Close();
                    }
                }

                bPluLoad = true;
                lblshow.Visible = false;
            }
            finally
            {
                CanDoNext = true;
            }
        }

        private void frmMain_Closing(object sender, CancelEventArgs e)
        {
            if (SmartVar.l_PluInfo != null)
            {
                SmartVar.l_PluInfo.Clear();
                SmartVar.l_PluInfo = null;
            }

            if (SmartVar.l_DspTruck != null)
            {
                SmartVar.l_DspTruck.Clear();
                SmartVar.l_DspTruck = null;
            }

            if (SmartVar.l_EtpCustInfo != null)
            {
                SmartVar.l_EtpCustInfo.Clear();
                SmartVar.l_EtpCustInfo = null;
            }

            if (SmartVar.l_UsrEmployee != null)
            {
                SmartVar.l_UsrEmployee.Clear();
                SmartVar.l_UsrEmployee = null;
            }
        }

        private void btnwslrtn_Click(object sender, EventArgs e)
        {
            if (!bPluLoad)
            {
                lblPluLoad.ForeColor = Color.Red;
                lblPluLoad.Text = "请先进行信息加载";
                lblPluLoad.Visible = true;
                return;
            }

            if (!CanDoNext)
            {
                return;
            }
            CanDoNext = false;

            try
            {
                frmWslTh frmwslth = new frmWslTh();
                try
                {
                    frmwslth.ShowDialog();
                }
                finally
                {
                    frmwslth = null;
                }
            }
            finally
            {
                CanDoNext = true;
            }
        }

        private void btnwsldh_Click(object sender, EventArgs e)
        {
            if (!bPluLoad)
            {
                lblPluLoad.ForeColor = Color.Red;
                lblPluLoad.Text = "请先进行信息加载";
                lblPluLoad.Visible = true;
                return;
            }

            if (!CanDoNext)
            {
                return;
            }
            CanDoNext = false;

            try
            {
                frmWslDh frmwsldh = new frmWslDh();
                try
                {
                    frmwsldh.ShowDialog();
                }
                finally
                {
                    frmwsldh = null;
                }
            }
            finally
            {
                CanDoNext = true;
            }
        }


        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (btnConnect.Text == "连接打印机")
            {
                strPrnName = SmartVar.PrinterName;
                Thread td = new Thread(new ThreadStart(RunConnect));
                td.Start();
                btnConnect.Text = "关闭打印机";
                if (langue == "zh-CN")
                    lblPluLoad.Text = "开始连接蓝牙打印机";
                else
                    lblPluLoad.Text = "Start searching printer";
                // 启动一个定时器来显示连接状态
                timercheck.Enabled = true;
                lblPluLoad.Visible = true;
            }
            else
            {
                SmartVar.myBT.HungConnect();
                btnConnect.Text = "连接打印机";
                timercheck.Enabled = false;
                lblPluLoad.Visible = false;
            }
        }

        private void RunConnect()
        {
            SmartVar.ConnectedBT = SmartVar.myBT.ConnectService(strPrnName, "1234");
        }

        private void timercheck_Tick(object sender, EventArgs e)
        {
            // 已连接就停止Timer
            if (SmartVar.ConnectedBT)
            {
                timercheck.Enabled = false;
            }
            if ((SmartVar.myBT.DeviceMessage_Ch != "") || (SmartVar.myBT.DeviceMessage_En != ""))
            {
                if (langue == "zh-CN")
                    lblPluLoad.Text = SmartVar.myBT.DeviceMessage_Ch;
                else
                    lblPluLoad.Text = SmartVar.myBT.DeviceMessage_En;
            }
        }
    }
}